package com.easylinkin.linkappapi.common.translate.config;

import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.std.BeanSerializerBase;

@Configuration
public class JacksonConfig {
	
	@Bean
	public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
		Jackson2ObjectMapperBuilderCustomizer cunstomizer = new Jackson2ObjectMapperBuilderCustomizer() {
			
			@Override
			public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
				jacksonObjectMapperBuilder.modulesToInstall(new SimpleModule() {
					private static final long serialVersionUID = 1L;

					@Override
					public void setupModule(SetupContext context) {
						super.setupModule(context);
						context.addBeanSerializerModifier(new BeanSerializerModifier() {
							@Override
							public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc,
									JsonSerializer<?> serializer) {
								if (serializer instanceof BeanSerializerBase) {
									return new CodeTranslateSerializer((BeanSerializerBase) serializer);
								}
								return serializer;
							}

						});
					}
				});
			}
		};
		
		return cunstomizer;
	}

}
